/*     */ package com.zimbra.cs.account.ldap.entry;
/*     */ 
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.DynamicGroup;
/*     */ import com.zimbra.cs.account.Entry.EntryType;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.GroupMembership;
/*     */ import com.zimbra.cs.account.Provisioning.MemberOf;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.ObjectType;
/*     */ import com.zimbra.cs.account.ldap.BySearchResultEntrySearcher;
/*     */ import com.zimbra.cs.account.ldap.BySearchResultEntrySearcher.SearchEntryProcessor;
/*     */ import com.zimbra.cs.account.ldap.LdapHelper;
/*     */ import com.zimbra.cs.account.ldap.LdapProvisioning;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ import java.util.Collection;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LdapDynamicGroup
/*     */   extends DynamicGroup
/*     */   implements LdapEntry
/*     */ {
/*     */   private final String dn;
/*     */   private DynamicUnit dynamicUnit;
/*     */   private StaticUnit staticUnit;
/*     */   
/*     */   public LdapDynamicGroup(String dn, String email, ZAttributes attrs, Provisioning prov)
/*     */     throws LdapException
/*     */   {
/*  57 */     super(email, attrs.getAttrString("zimbraId"), attrs.getAttrs(), prov);
/*  58 */     this.dn = dn;
/*     */   }
/*     */   
/*     */   public void setSubUnits(DynamicUnit dynamicUnit, StaticUnit staticUnit) {
/*  62 */     this.dynamicUnit = dynamicUnit;
/*  63 */     this.staticUnit = staticUnit;
/*     */   }
/*     */   
/*     */   public DynamicUnit getDynamicUnit() {
/*  67 */     assert (this.dynamicUnit != null);
/*  68 */     return this.dynamicUnit;
/*     */   }
/*     */   
/*     */   public StaticUnit getStaticUnit() {
/*  72 */     assert (this.staticUnit != null);
/*  73 */     return this.staticUnit;
/*     */   }
/*     */   
/*     */   public boolean hasExternalMembers() {
/*  77 */     return (this.staticUnit != null) && (this.staticUnit.hasExternalMembers());
/*     */   }
/*     */   
/*     */   public String getDN()
/*     */   {
/*  82 */     return this.dn;
/*     */   }
/*     */   
/*     */   public String[] getAllMembers() throws ServiceException
/*     */   {
/*  87 */     if (isMembershipDefinedByCustomURL()) {
/*  88 */       return ((LdapProvisioning)getProvisioning()).getNonDefaultDynamicGroupMembers(this);
/*     */     }
/*  90 */     return ((LdapProvisioning)getProvisioning()).getDynamicGroupMembers(this);
/*     */   }
/*     */   
/*     */   public Set<String> getAllMembersSet()
/*     */     throws ServiceException
/*     */   {
/*  96 */     return ((LdapProvisioning)getProvisioning()).getDynamicGroupMembersSet(this);
/*     */   }
/*     */   
/*     */   public String[] getAllMembers(boolean supportNonDefaultMemberURL)
/*     */     throws ServiceException
/*     */   {
/* 102 */     if (isMembershipDefinedByCustomURL()) {
/* 103 */       if (supportNonDefaultMemberURL) {
/* 104 */         return ((LdapProvisioning)getProvisioning()).getNonDefaultDynamicGroupMembers(this);
/*     */       }
/* 106 */       return new String[0];
/*     */     }
/*     */     
/*     */ 
/* 110 */     return getAllMembers();
/*     */   }
/*     */   
/*     */   public static String getDefaultDynamicUnitMemberURL(String zimbraId)
/*     */   {
/* 115 */     return String.format("ldap:///??sub?(zimbraMemberOf=%s)", new Object[] { zimbraId });
/*     */   }
/*     */   
/*     */   public static String getDefaultMemberURL(String zimbraId, String staticUnitZimbraId) {
/* 119 */     return String.format("ldap:///??sub?(|(zimbraMemberOf=%s)(zimbraId=%s))", new Object[] { zimbraId, staticUnitZimbraId });
/*     */   }
/*     */   
/*     */   public static class DynamicUnit extends NamedEntry implements LdapEntry
/*     */   {
/*     */     private final String dn;
/*     */     private final String emailAddr;
/*     */     
/*     */     public DynamicUnit(String dn, String name, ZAttributes attrs, Provisioning prov) throws LdapException
/*     */     {
/* 129 */       super(attrs.getAttrString("zimbraId"), attrs.getAttrs(), null, prov);
/* 130 */       this.dn = dn;
/* 131 */       this.emailAddr = attrs.getAttrString("mail");
/*     */     }
/*     */     
/*     */     public Entry.EntryType getEntryType()
/*     */     {
/* 136 */       return Entry.EntryType.DYNAMICGROUP_DYNAMIC_UNIT;
/*     */     }
/*     */     
/*     */     public String getDN()
/*     */     {
/* 141 */       return this.dn;
/*     */     }
/*     */     
/*     */     public String getEmailAddr() {
/* 145 */       return this.emailAddr;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class StaticUnit extends NamedEntry implements LdapEntry
/*     */   {
/*     */     public static final String MEMBER_ATTR = "zimbraMailForwardingAddress";
/*     */     private final String dn;
/*     */     
/*     */     public StaticUnit(String dn, String name, ZAttributes attrs, Provisioning prov) throws LdapException
/*     */     {
/* 156 */       super(attrs.getAttrString("zimbraId"), attrs.getAttrs(), null, prov);
/* 157 */       this.dn = dn;
/*     */     }
/*     */     
/*     */     public Entry.EntryType getEntryType()
/*     */     {
/* 162 */       return Entry.EntryType.DYNAMICGROUP_STATIC_UNIT;
/*     */     }
/*     */     
/*     */     public String getDN()
/*     */     {
/* 167 */       return this.dn;
/*     */     }
/*     */     
/*     */     private boolean hasExternalMembers() {
/* 171 */       return !getMembersSet().isEmpty();
/*     */     }
/*     */     
/*     */     public String[] getMembers() {
/* 175 */       return getMultiAttr("zimbraMailForwardingAddress");
/*     */     }
/*     */     
/*     */     public Set<String> getMembersSet() {
/* 179 */       return getMultiAttrSet("zimbraMailForwardingAddress");
/*     */     }
/*     */   }
/*     */   
/* 183 */   private static final Set<SearchDirectoryOptions.ObjectType> DYNAMIC_GROUPS_TYPE = Sets.newHashSet(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.dynamicgroups });
/* 184 */   private static final String[] BASIC_ATTRS = { "zimbraId", "zimbraIsAdminGroup", "memberURL" };
/*     */   
/*     */ 
/*     */   public static Provisioning.GroupMembership updateGroupMembershipForCustomDynamicGroups(LdapProvisioning prov, Provisioning.GroupMembership membership, Account acct, Domain domain, boolean adminGroupsOnly)
/*     */     throws ServiceException
/*     */   {
/* 190 */     String acctDN = prov.getDNforAccount(acct, null, false);
/* 191 */     if (acctDN == null) {
/* 192 */       return membership;
/*     */     }
/* 194 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().allDynamicGroups();
/* 195 */     ZLdapContext zlcCompare = null;
/*     */     try {
/* 197 */       zlcCompare = LdapClient.getContext(LdapServerType.get(false), LdapUsage.COMPARE);
/* 198 */       BySearchResultEntrySearcher searcher = new BySearchResultEntrySearcher(prov, (ZLdapContext)null, domain, BASIC_ATTRS, new GroupMembershipUpdator(prov, zlcCompare, acctDN, membership, adminGroupsOnly, true, false));
/*     */       
/*     */ 
/* 201 */       searcher.doSearch(filter, DYNAMIC_GROUPS_TYPE);
/*     */     } finally {
/* 203 */       LdapClient.closeContext(zlcCompare);
/*     */     }
/* 205 */     return membership;
/*     */   }
/*     */   
/*     */ 
/*     */   public static Provisioning.GroupMembership updateGroupMembershipForDynamicGroups(LdapProvisioning prov, Provisioning.GroupMembership membership, Account acct, Collection<String> ids, boolean adminGroupsOnly, boolean customGroupsOnly, boolean nonCustomGroupsOnly)
/*     */     throws ServiceException
/*     */   {
/* 212 */     if (ids.size() == 0) {
/* 213 */       return membership;
/*     */     }
/* 215 */     String acctDN = prov.getDNforAccount(acct, null, false);
/* 216 */     if (acctDN == null) {
/* 217 */       return membership;
/*     */     }
/* 219 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().dynamicGroupByIds((String[])ids.toArray(new String[0]));
/* 220 */     ZLdapContext zlcCompare = null;
/*     */     try {
/* 222 */       zlcCompare = LdapClient.getContext(LdapServerType.get(false), LdapUsage.COMPARE);
/* 223 */       BySearchResultEntrySearcher searcher = new BySearchResultEntrySearcher(prov, (ZLdapContext)null, (Domain)null, BASIC_ATTRS, new GroupMembershipUpdator(prov, zlcCompare, acctDN, membership, adminGroupsOnly, customGroupsOnly, nonCustomGroupsOnly));
/*     */       
/*     */ 
/* 226 */       searcher.doSearch(filter, DYNAMIC_GROUPS_TYPE);
/*     */     } finally {
/* 228 */       LdapClient.closeContext(zlcCompare);
/*     */     }
/* 230 */     return membership;
/*     */   }
/*     */   
/*     */   public static class GroupMembershipUpdator implements BySearchResultEntrySearcher.SearchEntryProcessor
/*     */   {
/*     */     private final LdapProvisioning prov;
/*     */     private final ZLdapContext zlcCompare;
/*     */     private final Provisioning.GroupMembership membership;
/*     */     private final boolean adminGroupsOnly;
/*     */     private final boolean customGroupsOnly;
/*     */     private final boolean nonCustomGroupsOnly;
/*     */     private final String acctDN;
/*     */     
/*     */     public GroupMembershipUpdator(LdapProvisioning prov, ZLdapContext zlcCompare, String acctDN, Provisioning.GroupMembership membership, boolean adminGroupsOnly, boolean customGroupsOnly, boolean nonCustomGroupsOnly)
/*     */     {
/* 245 */       this.prov = prov;
/* 246 */       this.zlcCompare = zlcCompare;
/* 247 */       this.acctDN = acctDN;
/* 248 */       this.membership = membership;
/* 249 */       this.adminGroupsOnly = adminGroupsOnly;
/* 250 */       this.customGroupsOnly = customGroupsOnly;
/* 251 */       this.nonCustomGroupsOnly = nonCustomGroupsOnly;
/*     */     }
/*     */     
/*     */     public void processSearchEntry(ZSearchResultEntry sr)
/*     */     {
/* 256 */       ZAttributes attrs = sr.getAttributes();
/* 257 */       String id = null;
/* 258 */       String isAdminStr = null;
/* 259 */       String memberURL = null;
/* 260 */       boolean isAdmin = false;
/*     */       try {
/* 262 */         id = attrs.getAttrString("zimbraId");
/* 263 */         isAdminStr = attrs.getAttrString("zimbraIsAdminGroup");
/* 264 */         memberURL = attrs.getAttrString("memberURL");
/* 265 */         isAdmin = isAdminStr == null ? false : "TRUE".equals(isAdminStr);
/*     */       } catch (LdapException e) {
/* 267 */         ZimbraLog.search.debug("Problem processing search result entry - ignoring", e);
/* 268 */         return;
/*     */       }
/* 270 */       if ((this.adminGroupsOnly) && (!isAdmin)) {
/* 271 */         return;
/*     */       }
/* 273 */       if ((this.nonCustomGroupsOnly) && (DynamicGroup.isMembershipDefinedByCustomURL(memberURL))) {
/* 274 */         return;
/*     */       }
/* 276 */       if ((this.customGroupsOnly) && (!DynamicGroup.isMembershipDefinedByCustomURL(memberURL))) {
/* 277 */         return;
/*     */       }
/*     */       try {
/* 280 */         if (this.prov.getHelper().compare(sr.getDN(), "member", this.acctDN, this.zlcCompare, false)) {
/* 281 */           this.membership.append(new Provisioning.MemberOf(id, isAdmin, true));
/*     */         }
/*     */       } catch (ServiceException e) {
/* 284 */         ZimbraLog.search.debug("Problem doing compare on group %s for member %s - ignoring", sr.getDN(), this.acctDN, e);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/entry/LdapDynamicGroup.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */